//
// Created by 高森森 on 2022/10/18.
//

#ifndef LEETCODE_SOLUTION_32_H
#define LEETCODE_SOLUTION_32_H
#include<bits/stdc++.h>
using namespace std;

class solution_32 {
    string longestCommonPrefix(vector<string>& strs) {
        if(!strs.size()){
            return "";
        }
        int minLength=INT_MAX;
        for(int i=0;i<strs.size();i++)
            minLength=min(minLength,(int)strs[i].size());
        int low=0,high=minLength;
        while(low<high){
            int mid=(high-low+1)/2+low;
            if(isComPrefix(strs,mid)){
                low=mid;
            }
            else{
                high=mid-1;
            }
        }
        return strs[0].substr(0,low);
    }
    bool isComPrefix(const vector<string>&strs,int length){
        string  str0=strs[0].substr(0,length);
        int count=strs.size();
        for(int i=1;i<count;i++)
        {
            string  str=strs[i];
            for(int j=0;j<length;j++){
                if(str0[j]!=str[j])
                    return false;
            }
        }
        return true;
    }
};


#endif //LEETCODE_SOLUTION_32_H
